String.prototype.trim = function(s){
	if(typeof(s)=='undefined')
		s = /(^\s*)|(\s*$)/g;
	else
		s = new RegExp(s, 'g');
    // 用正则表达式将前后空格
    // 用空字符串替代。
    return this.replace(s, "");
}

var post = $.post;
$.post = function(action, data, fn, json){
    if(typeof (json) == 'undefined'){
        json = "json";
    }
    var split = action.split("-");
    if(split[0] != 'undefined')
        action = APP_HOME + "/index.php?mod=" + split[0];
    if(split[1] != 'undefined')
        action += '&act=' + split[1];
    if(typeof fn == 'undefined'){
        var fn = function(data){}
    }
    var fun = function(data){
        fn(data);
    }
    post(action, data, fun, json);
}

//添加到收藏夹
function addBookmark(url,title) {
	if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
		return window.sidebar.addPanel(title,url,"");
	} else { //IE
		return window.external.AddFavorite(url,title);
	}
}


var msgbox = function(msg) {
    alert(msg);
}


var isInputEmpty = function(selector) {
    if($.trim($(selector).val()).length==0){
        msgbox('输入不能为空');
        $(selector)[0].focus();
        $(selector).css("background-color", "#eee");
    }
}


/**
 * Js简单模解析
 * @param string tpl 包含模板变量的字符串, 模板变量包含于 {: 与 } 之间，如 {:name} 就是变量 name
 * @param map vars 键/值, 如 { name : "小明" } 或 vars["name"] = "小明"
 */
function jsTpl(tpl, vars)
{
	var re, from, to;
	for(from in vars){
		re = new RegExp("\{:" + from + "\}", "g");
		to = vars[from];
		tpl = tpl.replace(re, to);
	}
	return tpl;
}


/**
 * 初始化输入框
 * @author lin
 * @param selector 输入框对象的选择器，跟jquery选择器规则一样
 * @param initContent （默认参数）输入框的初始化内容
 */
function initInput(selector, initValue)
{
	var obj;
	obj = $(selector);
	if(obj.length == 0){
		return ;
	}
	var initContent = (obj[0].defaultValue.length>0)? obj[0].defaultValue : initValue;//默认的输入框内容

	if(obj.val().trim().length == 0 && initContent.length>0){
		obj.val(initContent);
	}

	obj.blur(function(){//鼠标移出输入框时，如果文本框为空，则显示输入提示语
		if($.trim(this.value).length == 0){
			this.value = initContent;
		}
	}).click(function(){//当鼠标移动框入框时，如果输入框为空或者为提示输入语，则截为空
		if(this.value == initContent || $.trim(this.value).length == 0){
			this.value = '';
		}
	}).mouseover(function(){
		this.focus();
		this.select();
	});
}


/**
 * 提示框入框还可以输入多少个字
 * @author lin
 */
function checkInputCount(selector, maxLen, msgContainer)
{
	var countWord = function(){
		var obj = $(selector);
		var leftCount;
		leftCount = maxLen - obj.val().length;
		if(leftCount >= 0){
			$(msgContainer).html("你还能输入" + leftCount + "个字");
		}else{
			obj.val(obj.val().substr(0, maxLen-1));
			return false;
		}
	}

	$(selector).keydown(countWord).keyup(countWord);
}


//把指定下标的元素从数组中删除
function arrayRemove(arr, key)
{
	var arr2 = {};
	for(k in arr){
		if(k != key){
			arr2[k] = arr[k];
		}
	}
	return arr2;
}

/**
 * 给标有 var 属性的 html 元素赋值, 如 <input var='name' />, <span var='age'></span>
 */
function setVar(key, val, op)
{
	var t = typeof key;
	if(t == 'array' || t == 'object'){
		var obj = key;
		for(k in obj){
			setVar(k, obj[k]);
		}
	}
	var v = $("[var=" + key + "]");
	if(v.size()==0){
		return false;
	}
	if(v[0].tagName == 'INPUT' || v[0].tagName == 'TEXTAREA' || v[0].tagName == 'SELECT'){
		v.val(val);
	}else{
		v.html(val);
	}
}

function getVar(key)
{
	var v = $("[var=" + key + "]");
	if(v.size()==0){
		return 'undefined';
	}
	if(v[0].tagName == 'INPUT' || v[0].tagName == 'TEXTAREA' || v[0].tagName == 'SELECT'){
		return v.val();
	}else{
		return v.html();
	}
}

//Ajax进行删除操作, 服务器端删除数据后，在页面把数据删除
function ajaxDel(selector, action, data, fn)
{
	if(action.indexOf("/index.php/")==-1)
		action = "/index.php/" + action;
	data.ajax = 1;
	if(typeof fn != 'function'){
		fn = function(){};
	}
	$.post(
		action,
		data,
		function(d){
			if(typeof d.actionMsg != 'undefined' || typeof d.msg != 'undefined'){
				var msg = d.actionMsg || d.msg;
				if(typeof d.actionResult != 'undefined' && d.actionResult == false){
					showMsg(msg);
				}
			}
			fn();
			$(selector).fadeOut(500, function(){
				$(selector).remove();
			});
		},
		"json");
}

var quickMsg = function (msg, waitTime){
    alert(msg)
}